<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="编码名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入编码名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_flow_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="primary"-->
          <!--plain-->
          <!--icon="el-icon-plus"-->
          <!--size="mini"-->
          <!--@click="handleAdd"-->
          <!--v-hasPermi="['system:bigcustomer:add']"-->
        <!--&gt;新增</el-button>-->
      <!--</el-col>-->
      <div class="mydiv">
        <span>信息更新不及时请点击右侧刷新按钮一〉</span>
      </div>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bigcustomerList" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="编码名称" align="center" prop="customerName" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <!--<el-table-column label="客户创建时间" align="center" prop="customerCreateTime" width="180">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{ parseTime(scope.row.customerCreateTime, '{y}-{m}-{d}') }}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.state!=0">
            {{stateFormat(scope.row)}}
          </div>
          <div v-else>
            {{scope.row.taskName}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="2==scope.row.state && scope.row.delFlag != 1"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:bigcustomer:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="historyFory(scope.row)"
            v-hasPermi="['system:bigcustomer:edit']"
          >处理详情
          </el-button>

          <el-button v-if="0==scope.row.state"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="checkTheSchedule(scope.row)"
                     v-hasPermi="['system:bigcustomer:edit']"
          >查看进度
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      :visible.sync="progressopen"
      title="进度查询"
      width="1680px"
      append-to-body
    >
      <div style="position:relative;height: 100%;">
        <iframe
          id="iframe"
          :src="modelerUrl"
          frameborder="0"
          width="100%"
          height="720px"
          scrolling="auto"
        ></iframe>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="historyopen" width="1200px" append-to-body :close-on-click-modal="false">
      <handleHistoryForm :businessKey="businessKey" v-if="historyopen"/>
      <div slot="footer" class="dialog-footer">
        <el-button @click="historyopen=!historyopen">关闭</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改大客户流程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="编码" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBigcustomer, getBigcustomer, delBigcustomer, addBigcustomer, updateBigcustomer, exportBigcustomer } from '@/api/flow/bigcustomer';
import {getDefinitionsByInstanceId} from '@/api/activiti/definition'
import handleHistoryForm from "./handleHistoryForm";
import Cookies from 'js-cookie'
export default {
  name: "Bigcustomer",
  components: {handleHistoryForm},
  inject:['reload'],
  dicts: ['sys_flow_status'],
  data() {
    return {
      modelerUrl: '',
      userName: '',
      createName:'',
      businessKey: '',
      //用户信息
      user: {},
      // 状态字典
      stateOptions: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 大客户流程表格数据
      bigcustomerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      historyopen: false,
      progressopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: null,
        customerCreateTime: null,
        attachmentLink: null,
        instanceId: null,
        state: null,
        createName: null,
        lockFlag: null,
        abolishFlag: null,
        remarks: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerName: [
          {required: true, message: '客户名称不能为空', trigger: 'blur'}
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts('sys_flow_status').then(response => {
      this.stateOptions = response.data
    })
  },
  methods: {
    /** 查询大客户流程列表 */
    getList() {
      this.loading = true;
      listBigcustomer(this.queryParams).then(response => {
        console.log(response)
        this.bigcustomerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerName: null,
        customerCreateTime: null,
        attachmentLink: null,
        instanceId: null,
        state: null,
        createName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        lockFlag: null,
        abolishFlag: null,
        remarks: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.createName = Cookies.get("ReturnuserName");
      if (this.createName != "admin"){
        this.reset();
        this.open = true;
        this.title = "编码申请流程";
      }else{
        this.$alert('管理员不能创建流程', '管理员不能创建流程', {
          confirmButtonText: '确定',
        });
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      getBigcustomer(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改大客户流程";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBigcustomer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBigcustomer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除大客户流程编号为"' + ids + '"的数据项？').then(function() {
        return delBigcustomer(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 进度查看 */
    checkTheSchedule(row) {
      getDefinitionsByInstanceId(row.instanceId).then(response => {
        let data = response.data
        this.modelerUrl = '/bpmnjs/index.html?type=lookBpmn&instanceId=' + row.instanceId + '&deploymentFileUUID=' + data.deploymentID + '&deploymentName=' + encodeURI(data.resourceName);
        this.progressopen = true
      })
    },
    /** 审批详情 */
    historyFory(row) {
      this.businessKey = row.id
      this.historyopen = true
      this.title = '处理详情'
    },
    stateFormat(row, column) {
      return this.AcselectDictLabel(this.stateOptions, row.state)
    },
  }
};
</script>

<style lang="scss" scoped>
  .mydiv{
    width: 500px;
    height: 50px;
    position: absolute;
    left: 40%;
    top: 50%;
    margin: -10px 0 0 -50px;
  }
</style>
